package com.jinzhi.eventresource.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.eventresource.main.JzChargeRegion;
import com.jinzhi.eventresource.service.JzChargeRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <pre>
 * 金职资源库区域负责人controller
 * xzy
 * </pre>
 * <small> 2024-04-03 11:01:14 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/charge")
public class ChargeRegionController extends BaseController {

    @Autowired
    private JzChargeRegionService jzChargeRegionService;

    // list
    @ResponseBody
    @GetMapping("/list")
    public Result<Page<JzChargeRegion>> list(JzChargeRegion jzChargeRegion) {
        // 根据负责人姓名 省份 市 区 县模糊
        Wrapper<JzChargeRegion> wrapper = new EntityWrapper<JzChargeRegion>()
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getName()), "name", jzChargeRegion.getName())
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getProvince()), "province", jzChargeRegion.getProvince())
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getCity()), "city", jzChargeRegion.getCity())
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getDistrict()), "district", jzChargeRegion.getDistrict())
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getCounties()), "counties", jzChargeRegion.getCounties())
                .like(ObjectUtil.isNotEmpty(jzChargeRegion.getPhonenumber()), "phonenumber", jzChargeRegion.getPhonenumber())
                .orderBy("create_time", false);
        Page<JzChargeRegion> jzChargeRegionPage = jzChargeRegionService.selectPage(getPage(JzChargeRegion.class), wrapper);
        return Result.ok(jzChargeRegionPage);
    }

    // save
    @ResponseBody
    @PostMapping("/save")
    public Result<Boolean> save(JzChargeRegion jzChargeRegion) {
        jzChargeRegion.setCreateId(ShiroUtils.getUserId());
        jzChargeRegion.setCreateTime(LocalDateTime.now());
        boolean insert = jzChargeRegionService.insert(jzChargeRegion);
        return Result.ok(insert);
    }

    // edit
    @ResponseBody
    @PostMapping("/edit")
    public Result<Boolean> edit(JzChargeRegion jzChargeRegion) {
        jzChargeRegion.setUpdateId(ShiroUtils.getUserId());
        jzChargeRegion.setUpdateTime(LocalDateTime.now());
        boolean update = jzChargeRegionService.updateById(jzChargeRegion);
        return Result.ok(update);
    }

    // delete
    @ResponseBody
    @GetMapping("/delete")
//    @RequiresPermissions("jzweb:charge:delete")
    public Result<Boolean> delete(Long id) {
        boolean delete = jzChargeRegionService.deleteById(id);
        return Result.ok(delete);
    }


    @GetMapping()
    String EventresourceModule() {
        return "eventresource/resourceRobot/resourceRobot";
    }

    @GetMapping("/add")
    String add() {
        return "eventresource/resourceRobot/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Long id, Model model) {
        JzChargeRegion jzChargeRegion = jzChargeRegionService.selectById(id);
        model.addAttribute("jzChargeRegion", jzChargeRegion);
        return "eventresource/resourceRobot/edit";
    }

    @GetMapping("/info/{id}")
    String info(@PathVariable("id") Long id, Model model) {
        JzChargeRegion jzChargeRegion = jzChargeRegionService.selectById(id);
        model.addAttribute("jzChargeRegion", jzChargeRegion);
        return "eventresource/resourceRobot/info";
    }
}
